package org.sissgr.com{
	
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Resize;
	import mx.events.DragEvent;
	import mx.events.EffectEvent;
	import mx.events.MoveEvent;
	import mx.managers.CursorManager;
	
	import spark.effects.Fade;
	
	
	public class Windows extends Panel
	{
		[Bindable] public var showMaximize:Boolean = false;
		[Bindable] public var showMinimeze:Boolean = false;
		[Bindable] public var showResizer:Boolean = false;
		[Bindable] public var showClose:Boolean = false;
		
		[Embed(source="assets/img/resizeCursor.png")]
		private static var resizeCursor:Class;
		
		[Embed(source="assets/img/resizeHandler.png")]
		private static var resizeHandler:Class;
		
		[Embed(source="assets/img/closeButton.png")]
		private static var closeIco:Class;
		
		[Embed(source="assets/img/increaseButton.png")]
		private static var maxIco:Class;
		
		[Embed(source="assets/img/decreaseButton.png")]
		private static var restoreMaxIco:Class;
		[Embed(source="assets/img/minimizeButton.png")]
		private static var minimiseIco:Class;
		
		
		
		
		private var minimiseBtn:Button = new Button();
		private var closeBtn:Button = new Button();
		public var maximiseBtn:Button = new Button();
		private var resizerBtn:Image = new Image();
		
		
		private var effResize:Resize = new Resize();
		private var effMove:Move = new Move();
		private var effFade:Fade = new Fade();
		
		private var previousHeight:int = 25;
		private var previousWidth:int = 200;
		
		private var previousEspandH:Number = 600;
		private var previousEspandW:Number = 800;
		
		private var previousX:int = 0;
		private var previousY:int = 0;
		private var duracionEff:int = 200;
		private var	pTitleBar:UIComponent;
		private var resizeCur:Number = 0;
		private var cssUrl:URLRequest
		private var maximise:Boolean
		private var loader:URLLoader;
		
		
		public function Windows():void{
			super();
			super.verticalScrollPolicy = "Off";
			this.doubleClickEnabled = true;
			maximise = false;
		}
		
		
		
		
		override protected function createChildren():void{
			super.createChildren();
			addChildTitleBar();
			addListener();
		}
		
		private function removeChildTitleBar():void
		{
			this.titleBar.removeChild(maximiseBtn);
			this.titleBar.removeChild(minimiseBtn);
			this.titleBar.removeChild(closeBtn);
			this.rawChildren.removeChild(resizerBtn);
		}
		
		private function addChildTitleBar():void
		{
			if(showMaximize)
			this.titleBar.addChild(maximiseBtn);
			if(showMinimeze)
			this.titleBar.addChild(minimiseBtn);
			if(showClose)
			this.titleBar.addChild(closeBtn);
			if(showResizer)
			this.rawChildren.addChild(resizerBtn);
		}
		
		public function addListener():void {
			this.minimiseBtn.addEventListener(MouseEvent.CLICK, minimiseBtnClickHandler);
			this.maximiseBtn.addEventListener(MouseEvent.CLICK, maximiseBtnClickHandler)
			this.closeBtn.addEventListener(MouseEvent.CLICK,closeBtnClickHandler);
			this.titleBar.addEventListener(MouseEvent.DOUBLE_CLICK ,titleBarDoubleClickHandler);
			this.resizerBtn.addEventListener(MouseEvent.MOUSE_DOWN, resizeDownHandler);
			this.resizerBtn.addEventListener(MouseEvent.MOUSE_UP, removeCursorResize);
		}
		
		
		
		
		override protected function updateDisplayList(w:Number, h:Number):void{
			super.updateDisplayList(w,h);
			
			closeBtn.x = super.titleBar.width - 30;
			closeBtn.y = 5;
			closeBtn.width =25;
			closeBtn.height = 15;
			closeBtn.setStyle('icon',closeIco);
			
			maximiseBtn.x = super.titleBar.width - 60;
			maximiseBtn.y = 5;
			maximiseBtn.width = 25;
			maximiseBtn.height = 15;
			maximiseBtn.setStyle('icon',maxIco);
			
			
			minimiseBtn.x = super.titleBar.width - 90;
			minimiseBtn.y = 5;
			minimiseBtn.width = 25;
			minimiseBtn.height = 15;
			minimiseBtn.setStyle('icon',minimiseIco);
			
			resizerBtn.y = h - resizerBtn.height;
			resizerBtn.x = w - resizerBtn.width;
			resizerBtn.height=12;
			resizerBtn.width =12;
			resizerBtn.source = resizeHandler;
		}
		
		private function resizeDownHandler(e:MouseEvent):void{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, scalePanel);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopScale);
		}
		
		private function scalePanel(e:MouseEvent):void{
			if((stage.mouseX - x)>50)
				width = (stage.mouseX-x);
			if((stage.mouseY-y)>50)
				height = (stage.mouseY-y);   
			this.resizeCur = CursorManager.setCursor(resizeCursor);
		}
		
		private function removeCursorResize(e:MouseEvent):void{
			CursorManager.removeAllCursors();
		}
		
		
		private function stopScale(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, scalePanel);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopScale);
			CursorManager.removeAllCursors();
		}
		
		
		private function titleBarDoubleClickHandler(e:MouseEvent):void{
			this.titleBar.removeEventListener(MouseEvent.DOUBLE_CLICK ,titleBarDoubleClickHandler);
			this.titleBar.addEventListener(MouseEvent.DOUBLE_CLICK ,restoreCollapseHandler);
			collapse();
		}
		
		private function minimiseBtnClickHandler(e:MouseEvent):void{
			mimimise();
		}
		
		
		private function restoreCollapseHandler(e:MouseEvent):void{
			this.titleBar.removeEventListener(MouseEvent.DOUBLE_CLICK ,restoreCollapseHandler);
			this.titleBar.addEventListener(MouseEvent.DOUBLE_CLICK ,titleBarDoubleClickHandler);
			restoreCollapse();
		}
		
		private function maximiseBtnClickHandler(e:MouseEvent) :void{
			maximiseBtn.removeEventListener(MouseEvent.CLICK, maximiseBtnClickHandler)
			maximiseBtn.addEventListener(MouseEvent.CLICK, restoreMaximiseHandler)
			maximize();
		}
		
		private function restoreMaximiseHandler(e:MouseEvent) :void{
			maximiseBtn.removeEventListener(MouseEvent.CLICK, restoreMaximiseHandler)
			maximiseBtn.addEventListener(MouseEvent.CLICK, maximiseBtnClickHandler)
			restoreMaximise();
		}
		
		private function collapse():void
		{
			effResize.stop();
			effResize = new Resize();
			effResize.heightFrom = height;
			effResize.heightTo = previousHeight;
			effResize.duration=duracionEff;
			previousHeight = height;
			effResize.play([this]);
			removeChildTitleBar();
		}
		
		private function restoreCollapse():void
		{
			effResize.stop();
			effResize = new Resize();
			effResize.heightFrom = height;
			effResize.heightTo = previousHeight;
			effResize.duration=duracionEff;
			previousHeight = height;
			effResize.play([this]);
			addChildTitleBar();
		}
		
		
		private function maximize():void
		{
			effResize.stop();
			effResize = new Resize();
			effMove.stop();
			effMove = new Move();
			effMove.xTo = previousX;
			effMove.xFrom = x;
			effMove.yTo = previousY;
			effMove.yFrom = y;
			effMove.duration = duracionEff;
			previousX = x;
			previousY = y;
			effMove.play([this]);
			
			effResize.heightFrom = height;
			effResize.heightTo =  this.parent.height;
			effResize.widthFrom = 0;
			effResize.widthTo = this.parent.width;
			effResize.duration=0;
			previousEspandH = height;
			previousEspandW = width;
			effResize.play([this]);
			maximiseBtn.setStyle('icon',restoreMaxIco);
		}
		
		private function restoreMaximise():void
		{
			effResize.stop();
			effResize = new Resize();
			effMove.xTo = previousX;
			effMove.xFrom = x;
			effMove.yTo = previousY;
			effMove.yFrom = y;
			effMove.duration = duracionEff;
			previousX = x;
			previousY = y;
			effMove.play([this]);	
			
			effResize.heightFrom = height;
			effResize.heightTo =  previousEspandH;
			effResize.widthFrom = width;
			effResize.widthTo = previousEspandW;
			effResize.duration=duracionEff;
			previousEspandH = height;
			previousEspandW = width;
			effResize.play([this]);
		}
		
		private function mimimise():void
		{
				this.visible = false;
		}
		
		private function closeBtnClickHandler(e:MouseEvent):void
		{
			effFade = new Fade()
			effFade.alphaFrom = 1.0;
			effFade.alphaTo = 0.0;
			effFade.addEventListener(EffectEvent.EFFECT_END,closePanel);
			effFade.play([this]);
		}
		
		private function closePanel(e:Event):void
		{
			this.parent.removeChild(this);
		}
		
		
		
	}
	
	
}